package tq.com.tqcom.util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TQEventBus {

    static public void post(Object msg) {
        EventBus.getDefault().post(msg);
    }

    static private void register(Object pO){
        EventBus.getDefault().register(pO);
    }

    static private void unregister(Object pO){
        EventBus.getDefault().unregister(pO);
    }

    static public class TQEventMessage{
        private Opertor operator;
        private Object info;

        public enum Opertor{
            add,
            modify,
            delete
        }

        public TQEventMessage(Opertor pOperator, Object pInfo){
            this.operator = pOperator;
            this.info = pInfo;
        }

        public Opertor getOperator(){
            return operator;
        }

        public Object getInfo(){
            return info;
        }
    }

    public void register(){
        register(this);
    }
    public void unregister(){
        unregister(this);
    }

    public TQEventBus(){};
    public TQEventBus(IEventLisnter pEventLisnter){
        mEventLisnter = pEventLisnter;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TQEventMessage pEvent){
        if(mEventLisnter == null) return;
        mEventLisnter.onEvnet(pEvent);
    }

    public interface IEventLisnter{
        void onEvnet(TQEventMessage msg);
    }

    public void setEventLisnter(IEventLisnter pLisnter){
        mEventLisnter = pLisnter;
    }
    private IEventLisnter mEventLisnter = null;
}
